package com.axjy.yycg.web;

import com.axjy.yycg.pojo.Sysuser;
import com.axjy.yycg.service.SysuserService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginHandler {

    @Resource
    private SysuserService sysuserService;

    @GetMapping("/")
    public String login(){
        return "login";
    }
    @PostMapping("/login")
    public String login(String userid, String pwd, HttpSession session){
        Sysuser loginUser=sysuserService.login(userid,pwd);
        if(null==loginUser){
            throw new RuntimeException("用户名或密码错误");
        }

        session.setAttribute("loginUser",loginUser);

        return "redirect:/index";
    }
    @ExceptionHandler(RuntimeException.class)
    public String handlerException(RuntimeException re, HttpServletRequest req){
        req.setAttribute("loginErr",re.getMessage());
        return "login";
    }
}
